<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-09
 * Time: 14:09
 */

namespace app\api\validate\hotel;


use LinCmsTp5\validate\BaseValidate;

class CheckHotel extends BaseValidate
{
    protected $rule = [
        'name' => 'require|max:100',
        'title' => 'require|max:100',
        'price' => 'require',
        'introduce' => 'max:255',
        'basis' => 'checkBasis',
        'tag' => 'checkTagClaim',
        'facility' => 'checkFacility',
        'claim' => 'checkTagClaim',
    ];


    protected $message = [
        'name' => '请输入房间名称且长度不能大于100',
        'title' => '请输入房间标题且长度不能大于100',
        'price' => '请输入房间价格',
        'introduce' => '房间简介长度不能大于255',
        'basis' => '请选择输入正确的基础信息',
        'tag' => '请输入正确的标签信息',
        'facility' => '请选择正确的设施与服务',
        'claim' => '请填写正确的入住要求'
    ];

    // 基础信息
    protected function checkBasis($value)
    {
        if(count($value) > 0) {
            foreach ($value as $v) {
                if(!$this->isPositiveInteger($v['basis_id'])) {
                    return false;
                }
                if(strlen($v['name']) >= 50 || strlen($v['name']) == 0) {
                    return false;
                }
                if(strlen($v['explain']) >= 50 || strlen($v['explain']) == 0) {
                    return false;
                }
            }
        }
        return true;
    }

    // 标签信息 入住要求
    protected function checkTagClaim($value)
    {
        if(count($value) > 0) {
            foreach ($value as $v) {
                if (strlen($v['name']) >= 50 || strlen($v['name']) == 0) {
                    return false;
                }
            }
        }
        return true;
    }

    // 设施与服务
    protected function checkFacility($value)
    {
        if(count($value) > 0) {
            foreach ($value as $v) {
                if(!$this->isPositiveInteger($v['facility_id'])) {
                    return false;
                }
            }
        }
        return true;
    }

    protected function isPositiveInteger($value)
    {
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        } else {
            return false;
        }
    }
}